package com.cg.slms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cg.slms.domain.Client;
import com.cg.slms.domain.Employee;
import com.cg.slms.domain.Merchandise;
import com.cg.slms.domain.Stockin;
import com.cg.slms.domain.Stockorder;
import com.cg.slms.service.IClientService;
import com.cg.slms.service.IEmployeeService;
import com.cg.slms.service.IMerchandiseService;
import com.cg.slms.service.IStockinService;
import com.cg.slms.service.IStockorderService;
/**
 * 进货单控制器
 * @author GSC
 *
 */
@Controller
public class StockorderController {
	@Resource(name="stockorderService")
	IStockorderService stockorderService;
	public IStockorderService getStockorderService() {
		return stockorderService;
	}
	public void setStockorderService(IStockorderService stockorderService) {
		this.stockorderService = stockorderService;
	}

	@Resource(name="employeeService")
    IEmployeeService employeeService;
	@Resource(name="merchandiseService")
    IMerchandiseService merchandiseService;
	@Resource(name="clientService")
    IClientService clientService;
	public IEmployeeService getEmployeeService() {
		return employeeService;
	}
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	public IMerchandiseService getMerchandiseService() {
		return merchandiseService;
	}
	public void setMerchandiseService(IMerchandiseService merchandiseService) {
		this.merchandiseService = merchandiseService;
	}
	public IClientService getClientService() {
		return clientService;
	}
	public void setClientService(IClientService clientService) {
		this.clientService = clientService;
	}
	Map<Integer, String> employeesMap;
	Map<Integer, String> merchandisesMap;
	Map<Integer, String> clientsMap;
	/**
	 *     查询预处理
	 * @return ModelAndView
	 */
	@RequestMapping("/preStockorderAction")
	public ModelAndView preStockorderAction() {
		ModelAndView modelAndView;
		List<Employee> employees = employeeService.findEmployee(new Employee());
		List<Merchandise> merchandises = merchandiseService.findMerchandise(new Merchandise());
		List<Client> clients = clientService.findClient(new Client());
		if(employees!=null&&merchandises!=null&&clients!=null){
			System.out.println("控制器正在运行！！！");
			modelAndView=new ModelAndView("/stock/stockorder");
			employeesMap = new HashMap<Integer, String>();
			employeesMap.put(0, "");
			for (Employee employee : employees) {
				employeesMap.put(employee.getId(), employee.getName());
			}
			System.out.println(employeesMap);
			modelAndView.addObject("employees", employeesMap) ;
			clientsMap = new HashMap<Integer, String>();
			clientsMap.put(0, "");
			for (Client client : clients) {
				clientsMap.put(client.getId(), client.getName());
			}
			System.out.println(clientsMap);
			modelAndView.addObject("clients", clientsMap) ;
			merchandisesMap = new HashMap<Integer, String>();
			merchandisesMap.put(0, "");
			for (Merchandise merchandise : merchandises) {
				merchandisesMap.put(merchandise.getId(), merchandise.getName());
			}
			System.out.println(merchandisesMap);
			modelAndView.addObject("merchandises", merchandisesMap) ;
		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}
	/**
	 *   查询
	 * @param stockin
	 * @return ModelAndView
	 */
	@RequestMapping("/stockorderAction")
	public ModelAndView findStockorder(Stockorder stockorder) {
		System.out.println("控制器2在运行！！！！");
		ModelAndView modelAndView;
		List<Stockorder> stockorders = stockorderService.findStockorder(stockorder);
		if(stockorders!=null){
			modelAndView=new ModelAndView("/stock/stockorder");
			modelAndView.addObject("employees", employeesMap) ;
			modelAndView.addObject("merchandises", merchandisesMap) ;
			modelAndView.addObject("clients", clientsMap) ;
			modelAndView.addObject("stockorders", stockorders) ;
		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}
	/**
	 * 增加预处理
	 * @return ModelAndView
	 */
	
	  @RequestMapping("/preAddStockorderAction") 
	  public ModelAndView preAddStockorderAction() {
		  ModelAndView modelAndView; 
		  List<Employee> employees =employeeService.findEmployee(new Employee());
		  List<Merchandise> merchandises = merchandiseService.findMerchandise(new Merchandise());
		  List<Client> clients= clientService.findClient(new Client());
	      if(employees!=null&&merchandises!=null&&clients!=null){
		      System.out.println("控制器正在运行！！！"); 
			  modelAndView=new ModelAndView("/stock/addStockorder"); 
			  employeesMap = new HashMap<Integer,String>(); 
			  employeesMap.put(0, ""); 
			  for (Employee employee : employees) {
				  employeesMap.put(employee.getId(), employee.getName()); 
			  }
			  modelAndView.addObject("employees", employeesMap) ;
			  merchandisesMap = new HashMap<Integer, String>();  
			  merchandisesMap.put(0, ""); 
			  for (Merchandise merchandise : merchandises) { 
				  merchandisesMap.put(merchandise.getId(),merchandise.getName());
			  } 
			  modelAndView.addObject("merchandises",merchandisesMap) ; 
			  clientsMap = new HashMap<Integer, String>();
			  clientsMap.put(0, ""); 
			  for (Client client : clients) {
				  clientsMap.put(client.getId(), client.getName()); 
			  }
			  modelAndView.addObject("clients", clientsMap) ;
	     }else{ 
		      modelAndView=new ModelAndView("forward:/error.jsp"); 
		 } 
	     return modelAndView; 
 	}
	 
	/**
	 * 增加
	 * @param stockin
	 * @param errors
	 * @return
	 */
	@RequestMapping("/addStockorderAction")
	public ModelAndView addStockorder(@Valid Stockorder stockorder,Errors errors) {
		System.out.println("控制器2在运行！！！！");
		ModelAndView modelAndView;
		if (errors.getErrorCount()>0){
			modelAndView=new ModelAndView("/stock/addStockorder");
			modelAndView.addObject("employees", employeesMap) ;
			modelAndView.addObject("merchandises", merchandisesMap) ;
			modelAndView.addObject("clients", clientsMap) ;
			return modelAndView;
		}

		boolean res = stockorderService.addStockorder(stockorder);
		if(res){
			String finish_Url="preStockorderAction";
			modelAndView=new ModelAndView("forward:/finish.jsp");
			modelAndView.addObject("finish_Url", finish_Url) ;

		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}
	/**
	 * 搜索进货单by id
	 * @param id
	 * @return
	 */
	@RequestMapping("/findStockorderByIdAction")
	public ModelAndView findStockinById(String id) {
		ModelAndView modelAndView = new ModelAndView();
		System.out.println(id+"!!!");
		Stockorder stockorder= stockorderService.findStockorderById(id);
		if(stockorder!=null){
			  List<Employee> employees =employeeService.findEmployee(new Employee());
			  List<Merchandise> merchandises = merchandiseService.findMerchandise(new Merchandise());
			  List<Client> clients= clientService.findClient(new Client());
		      if(employees!=null&&merchandises!=null&&clients!=null){
			      System.out.println("控制器正在运行！！！"); 
				  modelAndView=new ModelAndView("/stock/updateStockorder"); 
				  employeesMap = new HashMap<Integer,String>(); 
				  employeesMap.put(stockorder.getEmployee().getId(), stockorder.getEmployee().getName()); 
				  System.out.println(stockorder.getEmployee().getId()+stockorder.getEmployee().getName());
				  for (Employee employee : employees) {
					  employeesMap.put(employee.getId(), employee.getName()); 
				  }
				  modelAndView.addObject("employees", employeesMap) ;
				  merchandisesMap = new HashMap<Integer, String>();  
				  merchandisesMap.put(stockorder.getMerchandise().getId(),stockorder.getMerchandise().getName()); 
				  System.out.println(stockorder.getMerchandise().getId()+stockorder.getMerchandise().getName());
				  for (Merchandise merchandise : merchandises) { 
					  merchandisesMap.put(merchandise.getId(),merchandise.getName());
				  } 
				  modelAndView.addObject("merchandises",merchandisesMap) ; 
				  clientsMap = new HashMap<Integer, String>();
				  clientsMap.put(stockorder.getClient().getId(), stockorder.getClient().getName()); 
				  System.out.println(stockorder.getClient().getId()+ stockorder.getClient().getName());
				  for (Client client : clients) {
					  clientsMap.put(client.getId(), client.getName()); 
				  }
				  System.out.println(clientsMap);
				  modelAndView.addObject("clients", clientsMap) ;
				  modelAndView.addObject("stockorder", stockorder) ;
		      }
		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}
/**
 * 修改进货单
 * @param stockin
 * @param errors
 * @return
 */
	@RequestMapping("/updateStockorderAction")
	public ModelAndView updateEmployee(@Valid Stockorder stockorder,Errors errors) {
		ModelAndView modelAndView;
		if (errors.getErrorCount()>0){
			modelAndView=new ModelAndView("/stock/updateStockorder");
			modelAndView.addObject("employees", employeesMap) ;
			modelAndView.addObject("merchandises", merchandisesMap) ;
			modelAndView.addObject("clients", clientsMap) ;
			modelAndView.addObject("stockorder",stockorder);
			return modelAndView;
		}
		boolean ret = stockorderService.updateStockorder(stockorder);
		if(ret){
			modelAndView=new ModelAndView("/stock/stockin");
			preStockorderAction();//重新执行预处理
			modelAndView.addObject("employees", employeesMap) ;
			modelAndView.addObject("merchandises", merchandisesMap) ;
			modelAndView.addObject("clients", clientsMap) ;
		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}
	/**
	 * 删除信息控制
	 * @param stockin
	 * @return
	 */
	@RequestMapping("/deleteStockorderByIdAction")
	public ModelAndView deleteStockinById(Stockorder stockorder) {
		ModelAndView modelAndView;
		boolean ret = stockorderService.deleteStockorderById(stockorder);
		if(ret) {
			String finish_Url="preStockorderAction";
			modelAndView=new ModelAndView("/stock/stockorder");
			modelAndView.addObject("employees", employeesMap) ;
			modelAndView.addObject("merchandises", merchandisesMap) ;
			modelAndView.addObject("clients", clientsMap) ;
			modelAndView.addObject("finish_Url", finish_Url) ;
		}else {
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}



}
